//
//  CNUserShopCarTools.m
//  BeeQuick_19
//
//  Created by w on 16/9/23.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNUserShopCarTools.h"
//导入字符串的分类文件
#import "NSString+Extension.h"
#import "CNGoodsModel.h"
#import "CNShoppingGoods.h"
// ---刘家强
#import "CNNavViewController.h"
#import "CNTabBarController.h"
#import <objc/runtime.h>

@implementation CNUserShopCarTools
//单例类方法
+(instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    static CNUserShopCarTools *shopCarTool;
    dispatch_once(&onceToken,^{
        if(shopCarTool == nil){
            shopCarTool = [CNUserShopCarTools new];
        }
    });
    return shopCarTool;
}
//模型数组
- (NSMutableArray <CNGoodsModel* >*)shopCar{
    if (!_shopCar) {
        _shopCar = [NSMutableArray array];
    }
    return  _shopCar;
}
#pragma mark 添加商品
- (void)addSuperMarkProductToShopCar:(CNGoodsModel *)goods{
    
//    [self setBadgeAndviewAnimation:self.shopCar.count];
    
    for (CNGoodsModel *obj in self.shopCar) {
        if (obj.goods_id == goods.goods_id) {
            return;
        }
    }
    //将商品添加到购物车
    [self.shopCar addObject:goods];
    
}
#pragma mark  移除商品
- (void)removeFromProductShopCar:(CNGoodsModel *)goods{
    for (CNGoodsModel *obj in self.shopCar) {
        if (obj.goods_id == goods.goods_id) {
            [self.shopCar removeObject:goods];
            //发送移除商品的通知
            [[NSNotificationCenter defaultCenter] postNotificationName:CNShopCarDidRemoveProductNSNotification object:self userInfo:nil];
            return ;
        }
    }
}
#pragma mark  获取商品价格
- (CGFloat)getShopCarGoodsPrice{//set方法
    __block double price = 0;
    [self.shopCar enumerateObjectsUsingBlock:^(CNGoodsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //商品价格一次叠加   doubleValue进行格式转换
//        NSString *str = [NSString stringWithFormat:@"%.2f",obj.partner_price];
//        price += [[str cleanDecimalPointZear]doubleValue];
    
    }];
    return price;
}
#pragma mark  获取商品数量
- (NSInteger)getShopCarGoodsNumber{//set方法
    __block NSInteger count = 0;
    [self.shopCar enumerateObjectsUsingBlock:^(CNGoodsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //用户每次购买的数量+1 用户对商品添加次数每次+1
 //       count += obj.userBuyNumber;
    }];
    return count;
}
//判断是否购物车是否有商品  没有则购物车数量为0
- (BOOL)isEmpty{
    return self.shopCar.count == 0;
}

// 添加运行时 获取属性
-(void)getAllMethods
{
    UInt32 mothCout_f =0;
    Method* mothList_f = class_copyMethodList(NSClassFromString(@"UITabBarButton"),&mothCout_f);
    for(int i=0;i<mothCout_f;i++)
    {
        Method temp_f = mothList_f[i];
        
        const char* name_s =sel_getName(method_getName(temp_f));
        
        NSString *ivarStr = [NSString stringWithCString:name_s encoding:NSUTF8StringEncoding];
        
        NSLog(@"___%@", ivarStr);
        
    }
}

@end
